﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect.Sql;



namespace DnDEngine
{
    public static class DnDEngine
    {
        public static List<Class.Website> LWebSites;
      //  public static List<

        public static void Start()
        {
            var data = new DbConnect.DataAccess().CreateDataBase();
            if (data == 1)
            {
                Logger.Log.TraceAction("DnDEngine.Start", "A new Database was created.");
            }
            else if (data == 2)
            {
                Logger.Log.TraceAction("DnDEngine.Start", "A Database was found.");
            }

            LoadData();
        }

        private static void LoadData()
        {
            try
            {
                Logger.Log.TraceAction("DnDEngine.LoadData", "");
                LWebSites = new List<Class.Website>();
                var data = new DbConnect.DataAccess();

                var req = SqlRequest.Select + SqlWebsite.ID + SqlRequest.From + SqlWebsite.TableName;
                var websites = data.Select(req);
                int ID = 0;
                foreach (System.Data.DataRow item in websites.Rows)
                {
                    ID = DbConnect.CoolFunctions.GetIntValue(item, "ID");

                    var web = new Class.Website(ID);

                    LWebSites.Add(web);
                }
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.LoadData", ex);
            }



        }

        #region "WebSite"
        public static bool AddWebSite(String Url, String MainPage, String Name)
        {
            var Ok = true;
            var resp = 0;
            try
            {
                Logger.Log.TraceAction("DnDEngine.AddWebSite", "");
                var web = new Class.Website(Name, Url);
                web.MainPage = MainPage;

                resp = web.Save();
                if (resp == 0)
                {
                    Ok = false;
                }
                else
                {
                    LWebSites.Add(web);
                }
               
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.AddWebSite", ex);
                Ok = false;
            }

            return Ok;
        }

        public static bool DeleteWebSite(int IDWebSite)
        {

            var Ok = false;
            try
            {
                Logger.Log.TraceAction("DnDEngine.DeleteWebSite", IDWebSite.ToString());
                var web = new Class.Website(IDWebSite);
                Ok = web.Delete();
                if (Ok)
                {
                    LWebSites.Remove(web); 
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.DeleteWebSite", ex);
                Ok = false;
            }
            finally
            {
            }
            return Ok;
        }

        public static bool UpdateWebsite(Class.Website Cpy)
        {

            var Ok = false;
            try
            {
                Logger.Log.TraceAction("DnDEngine.UpdateWebsite", "");
                var web = new Class.Website(Cpy.Index);
                Ok = Cpy.Update(Cpy);
                if (Ok)
                {
                    LWebSites.Remove(web);
                    LWebSites.Add(web);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.UpdateWebsite", ex);
                Ok = false;
            }
            finally
            {
            }
            return Ok;
        }

        public static Class.Website GetWebsite(int IDWebsite)
        {
            Class.Website web = null;
            try
            {
                Logger.Log.TraceAction("DnDEngine.GetWebsite", IDWebsite.ToString());
                web = new Class.Website(IDWebsite);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.GetWebsite", ex);

            }
            finally
            {
            }
            return web;
        }

        public static Boolean AddSiteType(String Name)
        {

            var Ok = false;
            try
            {
                Logger.Log.TraceAction("DnDEngine.AddSiteType", Name);
                
                Ok = true;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.AddSiteType", ex);
                Ok = false;
            }
            finally
            {
            }
            return Ok;
        }
        #endregion
        #region "Request"

        public static bool AddRequest(String Url, String WebSite)
        {
            var Ok = true;
            try
            {
                Logger.Log.TraceAction("DnDEngine.AddRequest", "");
                var req = new Class.Request(Url);
                foreach (var web in LWebSites)
                {
                    if (web.Name == WebSite)
                    {
                        req.WebSite = web.Index;
                    }
                }
                var resp = req.Create();

                if (resp == 0)
                {
                    Ok = false;
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.AddRequest", ex);
                Ok = false;
            }


            return Ok;
        }

        public static bool AddParam(int IDRequest, String Name, String Value)
        {
            var Ok = true;
            try
            {
                Logger.Log.TraceAction("DnDEngine.AddParam", "");
                var req = new Class.Request();
                req.Load(IDRequest);
                req.AddParam(Name, Value);
                Ok = true;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.AddParam", ex);
                Ok = false;
            }
            return Ok;
        }

        public static Class.Request GetRequest(Int16 IDRequest)
        {
            Class.Request req = null;
            try
            {
                Logger.Log.TraceAction("DnDEngine.GetRequest", IDRequest.ToString());
                req = new Class.Request(IDRequest);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("DnDEngine.GetRequest", ex);

            }
            finally
            {
            }
            return req;
        }


        #endregion
    }
}
